package com.ygqh.baby.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.constant.RedisConstant;
import com.ygqh.baby.model.CardCouponAction;
import com.ygqh.baby.redis.RedisDao;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.ygqh.baby.ao.CardMode;
import com.ygqh.baby.ao.CardType;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgCardCouponMapper;
import com.ygqh.baby.po.YgCardCoupon;
import com.ygqh.baby.po.YgCardCouponExample;
import com.ygqh.baby.service.YgCardCouponService;
import com.ygqh.baby.utils.DateConvertUtils;

@Service
public class YgCardCouponServiceImpl implements YgCardCouponService {

	@Autowired
	private YgCardCouponMapper ygCardCouponMapper;
	@Autowired
	private RedisDao redisDao;

	@Override
	public List<YgCardCoupon> find() {
		YgCardCouponExample example = new YgCardCouponExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygCardCouponMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgCardCouponExample example = new YgCardCouponExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygCardCouponMapper.countByExample(example);
	}

	@Override
	public YgCardCoupon findById(Long id) {
		return ygCardCouponMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgCardCoupon ygCardCoupon) {
		return ygCardCouponMapper.insertSelective(ygCardCoupon);
	}

	@Override
	public int update(YgCardCoupon ygCardCoupon) {
		return ygCardCouponMapper.updateByPrimaryKeySelective(ygCardCoupon);
	}

	@Override
	public int deleteSoft(Long[] ids, String handler) {
		Assert.notEmpty(ids);
		YgCardCouponExample example = new YgCardCouponExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgCardCoupon record = new YgCardCoupon();
		record.setStatus(DataStatus.Delete);
		record.setUpdateBy(handler);
		record.setUpdateTime(new Date());

		return ygCardCouponMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id, String handler) {
		return this.deleteSoft(new Long[]{id}, handler);
	}

	@Override
	public int saveOrUpdate(YgCardCoupon ygCardCoupon) {
		int r = 0;
		if (ygCardCoupon.getId() != null) {
			r = this.update(ygCardCoupon);
		} else {
			r = this.save(ygCardCoupon);
		}
		return r;
	}

	@Override
	public ResultSet<YgCardCoupon> search(QueryInfo queryInfo, CardType cardType, CardMode cardMode, Date startDate,
										  Date endDate, String q) {
		List<YgCardCoupon> list = ygCardCouponMapper.selectSuper(queryInfo, cardType, cardMode, startDate, endDate, q);
		int count = ygCardCouponMapper.countSuper(cardType, cardMode, startDate, endDate, q);
		ResultSet<YgCardCoupon> resultSet = new ResultSet<YgCardCoupon>(count, list);
		return resultSet;
	}

	@Override
	public Boolean exitCard(CardType cardType, CardMode cardMode, Date date) {
		YgCardCouponExample example = new YgCardCouponExample();
		example.createCriteria().andCardTypeEqualTo(cardType.name()).andCardModeEqualTo(cardMode.name())
				.andStartDateEqualTo(DateConvertUtils.getMonthStartDate(date))
				.andEndDateEqualTo(DateConvertUtils.getMonthEndDate(date));
		int i = ygCardCouponMapper.countByExample(example);
		return i > 0 ? true : false;

	}

	@Override
	public List<YgCardCoupon> findCardsByMonth(CardType cardType, CardMode cardMode, Date month) {
		Date startDate = null;
		Date endDate = null;
		if (month != null) {
			startDate = DateConvertUtils.getMonthStartDate(month);
			endDate = DateConvertUtils.getMonthEndDate(month);
		}
		return ygCardCouponMapper.selectCardsByMonth(cardType, cardMode, startDate, endDate);
	}

	@Override
	public YgCardCoupon getDrawPrizeCard(CardMode cardMode) {
		YgCardCouponExample example = new YgCardCouponExample();
		example.createCriteria().andCardTypeEqualTo(CardType.DrawPrize.name()).andCardModeEqualTo(cardMode.name())
				.andStatusEqualTo(DataStatus.Valid.name());

		List<YgCardCoupon> list = ygCardCouponMapper.selectByExample(example);
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Message validatePullNewCardCoupon() {
		List<YgCardCoupon> cardsByMonth = this.findCardsByMonth(CardType.PullNew, CardMode.Goods, null);
		if (CollectionUtils.isEmpty(cardsByMonth)) {
			return Message.error("亲，活动已结束", null);
		}
		Date now = new Date();
		YgCardCoupon ygCardCoupon = cardsByMonth.get(0);
		if (ygCardCoupon.getStartDate() != null && ygCardCoupon.getStartDate().after(now)) {
			return Message.error("亲，活动已结束", null);
		}
		if (ygCardCoupon.getEndDate() != null && ygCardCoupon.getEndDate().before(now)) {
			return Message.error("亲，活动未开始", null);
		}
		return Message.success(ygCardCoupon);
	}

	@Override
	public CardCouponAction getCardCouponActionInfoFromRedis() {
		String res = redisDao.get(RedisConstant.CARD_COUPON_GIFT_ACTION);
		if (StringUtils.isBlank(res)) {
			return null;
		}
		return JSON.parseObject(res, CardCouponAction.class);
	}

}
